package com.gzhryc.shared_device.oem.micro.queue.work;

import com.gzhryc.common.DateTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.shared_device.bxscn.code.dao.db.BXSCNShopOrder;
import com.gzhryc.shared_device.bxscn.code.dao.enums.BXSCNShopOrderEnum;
import com.gzhryc.shared_device.bxscn.code.services.BXSCNShopOrderService;
import com.gzhryc.shared_device.bxscn.code.services.dto.BXSCNShopOrderSearch;

import java.util.Date;
import java.util.List;

public class BXSCNShopOrderRevertTimeoutRunnable implements Runnable{

    static Logger log = Logger.getLogger(BXSCNShopOrderRevertTimeoutRunnable.class);

    @Override
    public void run() {
        log.info("便携式储能电池租借中订单归还超时，从租借中订单修改为待归还订单");
        Date date = new Date();
        BXSCNShopOrderSearch search = new BXSCNShopOrderSearch();
        search.setState(BXSCNShopOrderEnum.State.Lease.index());
        search.setPayState(BXSCNShopOrderEnum.PayState.DepositSuccess.index());
        search.setRevertTimeoutDate(date);

        Integer pageNo = 1;
        Integer pageLimit = 300;

        List<BXSCNShopOrder> shopOrderList = BXSCNShopOrderService.self(date).findBySearch(search,pageNo,pageLimit);
        while (shopOrderList != null && shopOrderList.size() > 0){
            for(BXSCNShopOrder shopOrder : shopOrderList){
                if(!DateTools.isSameDay(date,shopOrder.getEndDate())) {
                    BXSCNShopOrderService.self(date).leaseToWaitRevert(shopOrder.getTradeNo());
                }
            }

            pageNo = pageNo + 1;
            shopOrderList = BXSCNShopOrderService.self(date).findBySearch(search,pageNo,pageLimit);
        }
    }
}
